<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="700px"
    :close-on-click-modal="false"
    @close="close"
  >
    <el-form ref="form" label-width="70px" :model="form" :rules="rules">
      <el-form-item label="Key" prop="configKey">
        <el-input v-model.trim="form.configKey" :readonly="true" />
      </el-form-item>
      <el-form-item label="Value" prop="configValue">
        <el-input
          v-model.trim="form.configValue"
          :autosize="{ minRows: 4, maxRows: 6 }"
          type="textarea"
        />
      </el-form-item>
      <el-form-item label="描述" prop="configDescription">
        <el-input
          v-model.trim="form.configDescription"
          :autosize="{ minRows: 4, maxRows: 6 }"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { EditConfigInfo } from '@/api/projectConfig/configInfo'

  export default {
    name: 'EditConfigInfoForm',
    data() {
      return {
        form: {
          id: '',
          projectID: '',
          nameSpaceID: '',
          configKey: '',
          configValue: '',
          configDescription: '',
        },
        rules: {
          configKey: [
            { required: true, trigger: 'blur', message: 'Key值不能为空' },
          ],
          configValue: [
            { required: true, trigger: 'blur', message: 'Value值不能为空' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        // 修改前的配置值
        oldConfigValue: '',
      }
    },
    methods: {
      showForm(row) {
        this.title = '编辑'
        this.form = Object.assign({}, row)
        this.oldConfigValue = this.form.configValue
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            EditConfigInfo(this.form).then((res) => {
              if (res.data) {
                this.$baseMessage(res.msg, 'success')
                this.$emit('fetch-data', this.form.nameSpaceID)
                this.close()
              } else {
                this.$baseMessage(res.msg, 'error')
              }
            })
          }
        })
      },
    },
  }
</script>
